const jwt = require('jsonwebtoken');
const { JWT_SECRET, JWT_REFRESH_SECRET } = require('../config/config.default');
/**
 * @param {string} refreshToken
 */
async function Refresh(refreshToken) {
  try {
    const decoded = jwt.verify(refreshToken, JWT_REFRESH_SECRET);

    // 检查 refreshToken 是否过期
    if (decoded.exp < Date.now() / 1000) {
      throw new Error('Refresh token has expired');
    }
    const newAccessToken = jwt.sign(
      { userId: decoded.username }, // 传递用户ID或其他必要信息
      JWT_SECRET,
      { expiresIn: '15m' } // 设置新的 accessToken 的有效期
    );
    return newAccessToken;
  } catch (error) {
    throw error;
  }
}
module.exports = {Refresh}
